package cn.jimmie.learn.sdk.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.jimmie.learn.pbc.base.BaseFragment;
import cn.jimmie.learn.pbc.resource.PluginConst;
import cn.jimmie.learn.sdk.BuildConfig;
import cn.jimmie.learn.sdk.R;
import cn.jimmie.learn.sdk.async.LoadLocalPluginTask;
import cn.jimmie.learn.sdk.async.LoadRemotePluginTask;

/**
 * function :
 *
 * <p> Create Time:  2019年02月13日 17:58 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class LoadingFragment extends BaseFragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.sdk_fragment_loading, container, false);

        if (plugin().isAskUpdate() && BuildConfig.askUpdate) {
            // 校验插件
            checkAndLoadPlugin();
        } else {
            goPluginFragment();
        }
        return root;
    }

    private void checkAndLoadPlugin() {
        LoadLocalPluginTask task = new LoadLocalPluginTask(mContext);
        task.setListener(new LoadLocalPluginTask.OnLocalPluginListener() {
            @Override
            public void onLoadLocalPluginFinished(boolean useLocal) {
                if (useLocal) {
                    goPluginFragment();
                } else {
                    chooseUpdate();
                }
            }
        });
        task.execute();
    }

    private void chooseUpdate() {
        new AlertDialog.Builder(mContext)
                .setTitle("有插件可更新! 是否更新?")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        plugin().setAskUpdate(false);
                        goPluginFragment();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        loadPlugin();
                    }
                }).show();
    }

    private void loadPlugin() {
        LoadRemotePluginTask task = new LoadRemotePluginTask(mContext);
        task.setListener(new LoadRemotePluginTask.OnRemotePluginListener() {
            @Override
            public void onLoadRemotePluginFinished() {
                goPluginFragment();
            }
        });
        task.execute(true);
    }

    private void goPluginFragment() {
        mContext.usePluginResource(true);
        goFragment(PluginConst.FragmentName.MAIN, plugin().getPluginClassLoader());
    }
}
